package com.sanbuTech.util;

import java.io.Serializable;
import java.util.List;

public class Page<T> implements Serializable {

	private int pageSize = 15;
	private int pageIndex = 1; // 从0算起
	private long totalNum = -1;
	private List<T> result;

	// -- 构造函数 --//
	public Page() {
	}

	public Page(int pageSize) {
		this.pageSize = pageSize;
	}

	public Page(int pageIndex, int pageSize) {
		if(pageIndex != 0){
			this.pageIndex = pageIndex;
		}
		
		if(pageSize != 0){
			this.pageSize = pageSize;
		}
		
	}

	public long getTotalPages() {
		if (totalNum < 0) {
			return -1;
		}

		long count = totalNum / pageSize;
		if (totalNum % pageSize > 0) {
			count++;
		}
		return count;
	}

	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNext() {
		return (pageIndex + 1 <= getTotalPages());
	}

	/**
	 * 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
	 */
	public int getNextPage() {
		if (isHasNext()) {
			return pageIndex + 1;
		} else {
			return pageIndex;
		}
	}

	/**
	 * 是否还有上一页.
	 */
	public boolean isHasPre() {
		return (pageIndex - 1 >= 1);
	}

	/**
	 * 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
	 */
	public int getPrePage() {
		if (isHasPre()) {
			return pageIndex - 1;
		} else {
			return pageIndex;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public long getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(long totalNum) {
		this.totalNum = totalNum;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

}
